package com.wang.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wang.common.dao.impl.BaseDaoImpl;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IAStatResDao;
import com.wang.users.pojo.AStatRes;

/**
 * 管理员dao实现类
 * @author Zjx
 *
 */
public class AStatResDaoImpl extends BaseDaoImpl<AStatRes> implements IAStatResDao
{

	@Override
	public int saveOne(AStatRes statRes)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_stat_res (currDate,usersData,content,status,createTime,updateTime,pubTime) ");
		sb.append(" values ");
		sb.append(" (?,?,?,?,?,?,?) ");
		
		paramsList.add(statRes.getCurrDate());
		paramsList.add(statRes.getUsersData());
		paramsList.add(statRes.getContent());
		paramsList.add(statRes.getStatus());
		paramsList.add(statRes.getCreateTime());
		paramsList.add(statRes.getUpdateTime());
		paramsList.add(statRes.getPubTime());
		
		/**
		 * 执行sql语句
		 */
		int res =  executeSql(sb.toString(),paramsList);
		/* 处理主键 */
		int id = (int) paramsList.get(paramsList.size() - 1);
		statRes.setId(id);
		return res ; 
	}

	@Override
	public int updateOne(AStatRes statRes)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("update a_stat_res set ");
		sb.append(" currDate=?,usersData=?,content=?,status=?,createTime=?,updateTime=?,pubTime=? ");
		sb.append(" where id = ? ");
		
		paramsList.add(statRes.getCurrDate());
		paramsList.add(statRes.getUsersData());
		paramsList.add(statRes.getContent());
		paramsList.add(statRes.getStatus());
		paramsList.add(statRes.getCreateTime());
		paramsList.add(statRes.getUpdateTime());
		paramsList.add(statRes.getPubTime());
		paramsList.add(statRes.getId());
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public int deleteOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("delete from a_stat_res ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 不传条件不能删除 */
			return 0 ; 
		}
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public AStatRes findOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_stat_res where 1 = 1 ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" and (id  = ？ ) ");

			paramsList.add(condMap.get("id"));
		}else
		{
			return null ; 
		}
		/* 时间 */
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and currentDate >= ? and currentDate < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		
		return this.findOneSql(sb.toString(), paramsList,AStatRes.class);
	}

	@Override
	public List<AStatRes> findCondList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_stat_res where 1 = 1 ");
		
		/* 关键字 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sb.append(" and (email like ? or trueName like ? or phone like ? ) ");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sb.append(" and status = ?  ");
			paramsList.add(condMap.get("status"));
		}
		
		/* 时间 */
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and currentDate >= ? and currentDate < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		
		/* 排序 */
		if("idDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sb.append(" order by id asc ") ; 
		}else if("currentDateAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sb.append(" order by id currentDate desc ") ; 
		}else
		{
			sb.append(" order by pubTime asc ");
		}
		
		if(pageInfoUtil != null)
		{
			return this.findListSql(pageInfoUtil, sb.toString(), paramsList,AStatRes.class);
		}
		return this.findListSql(sb.toString(), paramsList,AStatRes.class); 
	}
}
